package com.athuiju.hr.service.impl;

import com.athuiju.hr.entity.Sector;
import com.athuiju.hr.entity.vo.SectorVo;
import com.athuiju.hr.mapper.SectorMapper;
import com.athuiju.hr.service.SectorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzh
 * @since 2021-12-15
 */
@Service
public class SectorServiceImpl extends ServiceImpl<SectorMapper, Sector> implements SectorService {

    @Autowired
    SectorMapper sectorMapper;


    /**
     * 添加部门*/
    @Override
    public boolean myAdd(SectorVo sectorVo) {
        Sector sector=new Sector();
        BeanUtils.copyProperties(sectorVo,sector);
        int insert = baseMapper.insert(sector);
        if (insert>0){
            return  true;
        }

        return false;
    }

    @Override
    public List<Sector> getOneSec() {
        return sectorMapper.getOneSec();
    }

    @Override
    public List<Sector> getBranById(String parentId) {
        return sectorMapper.getBranById(parentId);
    }

    @Override
    public List<Sector> getBranSonById(String parentId) {
        return sectorMapper.getBranSonById(parentId);
    }


    /**
     *修改状态*/
    @Transactional
    @Override
    public boolean myUpdateSectorByIds(List<String> ids, Integer state) {
        for (int i=0;i<ids.size();i++){
            Sector sector = baseMapper.selectById(ids.get(i));
            sector.setIsDisable(state==1?true:false);
            baseMapper.updateById(sector);
        }
        return true;
    }

    /**
     * 根据id集合删除*/
    @Transactional
    @Override
    public boolean myRemoveById(List<String> ids) {
        for (int i=0;i<ids.size();i++){
            baseMapper.deleteById(ids.get(i));
        }
        return true;
    }


    /**
     * 查询部门列表*/
    @Override
    public List<Sector> getThree() {
        return sectorMapper.getThree();
    }

    /**
     * 查询部门列表菜单结构*/
    @Override
    public List<Sector> queryThree() {
        QueryWrapper<Sector> wrapper = new QueryWrapper<Sector>();
        wrapper.orderByDesc("id");

        wrapper.select("id", "sector", "parent_id", "is_parent", "sort", "is_deleted","is_disable");
        List<Sector> sectors = baseMapper.selectList(wrapper);

        List<Sector> prosectors = buildsector(sectors);

        return prosectors;
    }


    /**
     * 递归遍历并树状菜单显示*/
    private List<Sector> buildsector(List<Sector> sectorsList) {
        List<Sector>finalNode=new ArrayList<>();
        for (Sector sectorNode:sectorsList){
            if ("0".equals(sectorNode.getParentId().toString())){
                sectorNode.setLevel(1);
                finalNode.add(selectChildren(sectorNode,sectorsList));
            }
        } Collections.sort(finalNode);
        return  finalNode;

    }

    private Sector selectChildren(Sector sectorNode, List<Sector> sectorsList) {
        System.out.println("selectChildren:sectorsList"+ sectorsList.size());
        sectorNode.setChildren(new ArrayList<Sector>());
        for (Sector it:sectorsList){
            if (sectorNode.getId().toString().equals(it.getParentId().toString())){
                int level=sectorNode.getLevel()+1;
                it.setLevel(level);
                if (sectorNode.getChildren()==null){
                    sectorNode.setChildren(new ArrayList<Sector>());
                }
                sectorNode.getChildren().add(selectChildren(it,sectorsList));
                Collections.sort(sectorNode.getChildren());
                //当Level为3时变返回为空
                if (sectorNode.getLevel()==3){
                    sectorNode.setChildren(null);
                }if (it.getLevel()==3){
                    it.setChildren(null);
                }
            }
        }
        return sectorNode;
    }


}
